//
//  main.cpp
//  lock_guard
//
//  Created by huge on 2021/6/22.
//  Copyright © 2021 huge. All rights reserved.
//

#include <iostream>
#include <thread>
#include <mutex>
#include <stdexcept>

std::mutex mtx;

void print_thread_id(int id){
    mtx.lock();
    std::lock_guard<std::mutex> lck(mtx,std::adopt_lock);
    std::cout<<"thread #" << id << std::endl;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    
    std::thread threadArray[10];
    for (int i=0; i<10; i++) {
        threadArray[i] = std::thread(print_thread_id,i);
//      static_cast<std::thread>(threadArray[i]).join();
    }
    
    for (auto& th :threadArray) {
        th.join();
        std::cout << &th <<std::endl;
    }
    
    std::cout << "Hello, World!\n";
    return 0;
}
